/**
 * 
 */
package org.crystal.jqweb.json;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crystal.jqweb.db.Column;
import org.crystal.jqweb.util.BeanUtils;

/**
 * @author Crystal
 *
 */
public class JQGridJsonSerialize {
	private final Log log = LogFactory.getLog(getClass());
	int currentPage=0;
	int totalPage=0;
	int records=0;
	/**
	 * 
	 */
	public JQGridJsonSerialize() {
		// TODO Auto-generated constructor stub
		
	}
	public JQGridJsonSerialize(int currentPage,int pageResults,int recordsCount) {
		// TODO Auto-generated constructor stub
		this.currentPage=currentPage;
		this.totalPage=(recordsCount%pageResults>0)?recordsCount/pageResults+1:recordsCount/pageResults;
		this.records=recordsCount;
	}
	
	public <T> String toJsonString(T bean){
		log.debug("toJsonString json bean Class is:"+bean.getClass());
		
		if(bean.getClass().isArray()){
			log.debug("toJsonString json bean Class is:"+bean.getClass().isArray());
			//return arrayToJsonString(bean);
		}
		String id="\"id\":";
		StringBuffer sbJson=new StringBuffer("[");
		Field [] fields = bean.getClass().getDeclaredFields();
		 for (Field field : fields) {
          	if (field.isAnnotationPresent(Json.class)) {
          		
          		if(field.getName().equals("id")){
          			id+="\""+BeanUtils.get(bean, field.getName()).toString()+"\"";
          		}
	          	if(BeanUtils.get(bean, field.getName())!=null){
	          		if(sbJson.toString().equals("[")){
	          			sbJson.append("\""+BeanUtils.get(bean, field.getName()).toString()+"\"");
	          		}else{
	          			sbJson.append(","+"\""+BeanUtils.get(bean, field.getName()).toString()+"\"");
	          		}
	          	}else{
	          		if(sbJson.toString().equals("[")){
		          			sbJson.append("\"\"");
		          		}else{
		          			sbJson.append(","+"\"\"");
		          		}
		          	}
          	}
		 }
		 sbJson.append("]");
		 log.debug("toJsonString() sbJson:"+"{"+id+",\"cell\":"+sbJson.toString()+"}");
		return "{"+id+",\"cell\":"+sbJson.toString()+"}";
	}
	
	public <T> String toJsonString(List <T> list){
		log.debug("toJsonString json List  Size is:"+list.size());
		StringBuffer sbJson=new StringBuffer("[");
		Iterator<T> itList=list.iterator();
		while(itList.hasNext()){
			if(sbJson.toString().equals("[")){
      			sbJson.append(getValueJsonString(itList.next()));
      		}else{
      			sbJson.append(","+getValueJsonString(itList.next()));
      		}
		}	
		sbJson.append("]");
		log.info("toJsonString() sbJson:"+"\"rows\":"+sbJson.toString());
		return "{\"page\":\""+currentPage+"\",\"total\":\""+totalPage+"\",\"records\":\""+records+"\",\"rows\":"+sbJson.toString()+"}";
	}
	
	private <T> String getValueJsonString(T value){
		String jsonString="";
		String fieldValue="";
		
		String fieldType=value.getClass().getName().toString();
		String fillValue=value.toString();
		if(fieldType.equals("java.lang.Object")){
        	if(value!=null){
        		fieldValue=toJsonString(value);
        	}
			log.debug("getObjectJsonString() Class  is Object  invoke toJsonString()");
        }else if((value.getClass().getSuperclass().getName().toString()).equals("java.lang.Object")){
        	if(value!=null){
        		fieldValue=toJsonString(value);
        	}
			log.debug("getObjectJsonString() Superclass  is Object  invoke toJsonString()");
        }
		
		jsonString+=""+fieldValue+"";
		
		log.debug("getObjectJsonString() jsonString:"+jsonString);
		log.debug("getObjectJsonString() Object Type:"+fieldType+",Value:"+fillValue+",fieldValue:"+fieldValue);
		return jsonString;
	}
}
